package com.yyn.Learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyn.Learning.entity.EduTeacher;
import com.yyn.Learning.entity.vo.TeacherQuery;
import com.yyn.Learning.mapper.EduTeacherMapper;
import com.yyn.Learning.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyn.R;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author yyn
 * @since 2022-11-08
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
   @Autowired
   EduTeacherMapper eduTeacherMapper;
    @Override
    public R page(Long page, int pageSize, TeacherQuery teacherQuery) {
        Page<EduTeacher> Page = new Page<>(page, pageSize);
        LambdaQueryWrapper<EduTeacher> lambdaQueryWrapper = new LambdaQueryWrapper<EduTeacher>();
//如果teacherQuery不为空
        lambdaQueryWrapper.like( StringUtils.isNoneEmpty(teacherQuery.getName()),EduTeacher::getName, teacherQuery.getName());
        lambdaQueryWrapper.like(!StringUtils.isNoneEmpty(String.valueOf(teacherQuery.getLevel())), EduTeacher::getLevel, teacherQuery.getLevel());
        lambdaQueryWrapper.ge(!StringUtils.isEmpty((CharSequence) teacherQuery.getStart()), EduTeacher::getGmtCreate, teacherQuery.getStart());
        lambdaQueryWrapper.le(!StringUtils.isEmpty((CharSequence) teacherQuery.getEnd()), EduTeacher::getGmtCreate, teacherQuery.getEnd());

        lambdaQueryWrapper.orderByDesc(EduTeacher::getSort);
        this.page(Page, lambdaQueryWrapper);
        return R.success().data("total", Page.getTotal()).data("rows", Page.getRecords());
    }

    @Override
    public R dele(String id) {
        this.removeById(id);
        return R.success().message("已删除");
    }

    @Override
    public R insert(@RequestBody EduTeacher eduTeacher) {
      boolean save= this.save(eduTeacher);
      if (save){
          return R.success().message("新增成功");
      }else {
          return R.error().message("新增失败");
      }
    }

    @Override
    public R upDate(@RequestBody EduTeacher eduTeacher) {
    boolean  upDate=  this.updateById(eduTeacher);
        if (upDate){
            return R.success().message("修改成功");
        }else {
            return R.error().message("修改失败");
        }
    }

    @Override
    public R selectById(String id) {
        LambdaQueryWrapper<EduTeacher> eduTeacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduTeacherLambdaQueryWrapper.eq(EduTeacher::getId,id);
        EduTeacher selectById = this.getOne(eduTeacherLambdaQueryWrapper);
        return R.success().data("teacher",selectById);
    }
    @Override
    @ApiOperation("查询所有")
    public R selectAll(){
        List<EduTeacher> allList = this.list();
        return R.success().data("allList",allList);

    }

    @Override
    public List<EduTeacher> getRecommendTeacher() {
        List<EduTeacher> recommendTeacher = eduTeacherMapper.getRecommendTeacher();
        return recommendTeacher;
    }
}
